"""
脚本输入：A_porps.txt
脚本输出：result1.xlsx
"""

import pandas as pd
file_path = r"C:\Users\袁瑞浩\Desktop\A_porps.txt"
YOUNGMOD = []
POISSONR = []
with open(file_path, 'r', encoding='utf-8') as file:
    for line in file:
        line = line.strip()  # line 形式为 "n*m, n*m, ..."去除了每一行的首尾空白
        if line == 'YOUNGMOD':
            current_data = 'YOUNGMOD'
        elif line == 'POISSONR':
            current_data = 'POISSONR'
        elif line == '/':
            continue
        elif line == '':
            continue
        else:
            for item in line.split():  # item 形式为 "n*m"
                num = int(item.split('*')[0])  # num就是n
                if current_data == 'YOUNGMOD':
                    youngmod = float(item.split('*')[1])  # 杨氏模量具体数据
                if current_data == 'POISSONR':
                    poissonr = float(item.split('*')[1])  # 泊松比具体数据
                if current_data == 'YOUNGMOD':
                    for i in range(num):
                        YOUNGMOD.append(youngmod)
                if current_data == 'POISSONR':
                    for j in range(num):
                        POISSONR.append(poissonr)

# 将两个列表合并成一个字典
data = {
    'YOUNGMOD': YOUNGMOD,
    'POISSONR': POISSONR
}
# 创建 DataFrame
df = pd.DataFrame(data)
# 保存到 Excel 文件，指定浮点数格式
excel_file = r"D:\原子比赛\杨泊数据重新处理.xlsx"
df.to_excel(excel_file, index=False, float_format="%.30f")  # 保留 8 位小数
print(f"数据已保存到 {excel_file}")
